package com.zusmart.core.plugin.extend;

import java.lang.annotation.Annotation;

import com.zusmart.base.logging.Logger;
import com.zusmart.base.logging.LoggerFactory;
import com.zusmart.core.bean.BeanDefinitionRegistry;
import com.zusmart.core.context.ApplicationContext;
import com.zusmart.core.plugin.Plugin;
import com.zusmart.core.plugin.PluginManager;

public class EnableShutdownHookPlugin extends Plugin implements Runnable {

	private static final Logger logger = LoggerFactory.getLogger(EnableShutdownHookPlugin.class);

	public EnableShutdownHookPlugin(Annotation annotation, PluginManager pluginManager, BeanDefinitionRegistry beanDefinitionRegistry, ApplicationContext applicationContext) {
		super(annotation, pluginManager, beanDefinitionRegistry, applicationContext);
	}

	@Override
	protected void doStart() throws Exception {
		Runtime.getRuntime().removeShutdownHook(new Thread(this, "shutdown-hook"));
	}

	@Override
	public void run() {
		try {
			this.getApplicationContext().close();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

}